#ifndef SERVER_H
#define SERVER_H

#include <sqlite3.h>
#include "../common/common.h"
#include "../common/protocol.h"

#define MAX_CLIENTS 100
#define PORT 8888

// 客户端结构
typedef struct {
    int socket;
    User user;
    bool authenticated;
    pthread_t thread_id;
} Client;

// 全局客户端列表
extern Client clients[MAX_CLIENTS];
extern pthread_mutex_t clients_mutex;
extern sqlite3 *db;

// 数据库初始化
void init_database();

// 消息处理函数
void handle_login(int client_socket, Message *msg, User *current_user);
void handle_register(int client_socket, Message *msg);
void handle_logout(int client_socket, User *current_user);
void handle_get_online_users(int client_socket);
void handle_request_game(int client_socket, Message *msg);
void handle_respond_game(int client_socket, Message *msg);
void handle_move(int client_socket, Message *msg);
void handle_promotion(int client_socket, Message *msg);
void handle_game_over(int client_socket, Message *msg);
void handle_resign(int client_socket, Message *msg);
void handle_cancel_game(int client_socket, Message *msg);

// 客户端管理
void add_client(int socket);
void remove_client(int socket);
int find_client_by_user_id(int user_id);

// 游戏房间管理
void create_game_room(int player1_id, int player2_id);
void remove_game_room(int room_id);
int find_game_room_by_player(int player_id);
int find_opponent_in_room(int room_id, int player_id);

#endif
